#@layout()

#define js()
    <script>
        $(function () {
            $("input:checkbox[name=permission]").change(function() {
                var roleStr=$("#code").val();
                var permission={"role":roleStr,"permissions":[]}
                $('input:checkbox[name=permission]:checked').each(function (i) {
                    permission.permissions.push(roleStr+":"+$(this).val());
                });
                var str = JSON.stringify(permission);
                $("#result").val(str);
            });
            var permissionVal=$("#result").val();
            var roleStr=$("#code").val();
            var permissionArr=permissionVal.split(",");
            for(var i=0;i<permissionArr.length;i++){
                result=permissionArr[i].replace(roleStr+":","");
                $("input:checkbox[value='"+result+"']").attr('checked','true');
            }

        });

        $("#role-dipatch").validate({
            rules:{},
            messages:{},
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var config = {
                    "url":"#(contextPath)/admin/role/roleDispatch",
                    data:{"permission":$("#result").val()},
                    "method":"POST"
                };
                var backendTool = new BackendTool(config);
                backendTool.ajaxRequest();
            }
        });
    </script>
#end


#define main()
    <form class="form form-horizontal" id="role-dipatch">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色名称：</label>
            <div class="formControls col-xs-8 col-sm-9">#(role.name??'')
                <input type="hidden" value="#(role.code??'')" id="code">
            </div>
        </div>
        #for(m:moduleList)
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">#(m.name??"")：</label>
            <div class="formControls col-xs-8 col-sm-9">
                #for(p:m.modulePermission)
                    <input type="checkbox" name="permission" value="#(p.permission??'')">#(p.name??"")
                #end
            </div>
        </div>
        #end
        <div class="row cl" style="display:none;">
            <label class="form-label col-xs-4 col-sm-3">JSON：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <textarea id="result" name="permissionJson">#(rolePermission.permission??"")</textarea>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input type="hidden" value="#(role.id??'')" id="id" name="role.id">
                <input class="btn btn-primary radius" type="submit" value="  提交  ">
            </div>
        </div>
    </form>
#end